<template>
  <div class="dashboard-container body-min-width">
    <h2 class="head-big-title">
      Laporan Jualan
      <el-date-picker
        style="float: right;"
        v-model="dateTime"
        type="daterange"
        @change="getResetLoadData"
        align="right"
        unlink-panels
        format="yyyy-MM-dd"
        value-format="yyyy-MM-dd"
        range-separator="To"
        start-placeholder="start"
        end-placeholder="end">
      </el-date-picker>
    </h2>
    <div style="width: 92%;height: 120px;margin: 20px 4% 0 4%;padding: 10px 0 0 20px;border-radius: 10px;background-color: white;">
      <el-form label-position="top" label-width="80px" :inline="true" class="head-search" :model="searchData" style="float: left;text-align: left;">
        <el-form-item label="Dealer Utama / Dealer" width="188">
          <el-input v-model="searchData.dealerName" @change="getResetLoadData"></el-input>
        </el-form-item>
        <el-form-item label="Status Pesanan" width="188">
          <el-select v-model="searchData.orderState" @change="getResetLoadData" class="input-width" placeholder="Status">
            <el-option v-for="(item, index) in $orderStateList" :key="index" :label="item.label" :value="item.value" v-show="!(item.value == -2)"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <h2 style="line-height: 100px;text-align: right;margin-right: 66px;">Jumlah Jualan: {{$formatMoneyStr(totalSales)}}</h2>
    </div>
    <el-table
      v-loading="loading"
      :data="tableData"
      style="width: 92%;margin: 20px 4% 0 4%;padding-left: 10px;border-radius: 20px;">
      <el-table-column
        label="#"
        type="index">
      </el-table-column>
      <el-table-column prop="dealerName" label="Nama Dealer Utama" align="center"></el-table-column>
      <el-table-column prop="totalPOs" label="Jumlah PO"></el-table-column>
      <el-table-column prop="amt" label="Jumlah"></el-table-column>
      <el-table-column label="Status">
        <template slot-scope="scope">
          <p v-for="(item,index) in $orderStateList" :key="index" v-show="item.value == scope.row.state">
            {{item.label}}</p>
        </template>
      </el-table-column>
      <el-table-column label="Tindakan" width="128" align="center" v-if="showActionButton">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            style="font-size: 18px;padding: 0;background-color: rgb(56,96,134);border: 0;width: 24px;height: 24px;"
            @click="showDetail(scope.row)"><svg-icon icon-class="ico_view_dealer"></svg-icon></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      style="float: right;margin: 16px 4% 10px"
      layout="prev, pager, next"
      @current-change="handleCurrentChange"
      :page-size="searchData.pageNum"
      :current-page="searchData.pageIndex"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { salesAll_master_dealerApi } from '@/api/user'

export default {
  name: 'Customers',
  computed: {
    ...mapGetters([
      'role'
    ])
  },
  data() {
    return {
      loading: false,
      showActionButton: true,
      tableData: [],
      dateTime: [],
      searchData: {
        orderState: 5,
        pageNum: 10,
        pageIndex: 1
      },
      total: 0,
      totalSales: 0
    }
  },
  created() {
    this.checkResetTime()
    this.loadData()
  },
  mounted() {
  },
  methods: {
    checkResetTime() {
      const now = new Date()
      let monthStr = now.getMonth() + 1
      let startTime = ''
      let endTime = ''
      endTime = now.getFullYear() + ((monthStr < 10) ? '-0' : '-') + monthStr + '-' + now.getDate()
      startTime = ''
      if (now.getDate() < 10) { // 10日前
        if (monthStr == 1) {
          startTime = (now.getFullYear() - 1) + '-12' + '-' + now.getDate()
        } else {
          monthStr = monthStr - 1
          startTime = now.getFullYear() + ((monthStr < 10) ? '-0' : '-') + monthStr + '-' + now.getDate()
        }
      } else {
        startTime = now.getFullYear() + ((monthStr < 10) ? '-0' : '-') + monthStr + '-1'
      }
      this.dateTime = [startTime, endTime]
    },
    getResetLoadData() {
      this.searchData.pageIndex = 1
      this.loadData()
    },
    showDetail(row) {
      this.showActionButton = false
      this.searchData.dealerId = row.dealerId
      this.loadData()
    },
    loadData() {
      let requestData = this.searchData
      requestData.startTime = this.dateTime[0]
      requestData.endTime = this.dateTime[1]
      salesAll_master_dealerApi(requestData).then(res => {
        this.tableData = res.data.tradDetail.records
        this.total = res.data.tradDetail.total
        this.totalSales = res.data.totalSales
      }).catch(err => {
        console.log(err)
      })

    }, // 得到数据表格
    handleEdit(val) {
    },
    handleCurrentChange(val) {
      this.searchData.pageIndex = val
      this.loadData()
    }
  }
}
</script>

<style lang="scss" scoped>
  .head-big-title{
    text-align: left;
    font-size: 20px;
    margin: 20px 0 0 3%;
  }
  .dashboard-container{
    margin-top: 20px;
    text-align: center;
  }

</style>
<style scoped>
  /deep/.head-search label{
    line-height: 30px;
  }
</style>
